package org.zhq.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhq.ServerResult;
import org.zhq.pojo.bo.ShopCartBO;
import org.zhq.service.ShopCartService;

@Api(value = "购物车接口", tags = {"购物车相关接口"})
@RestController
@RequestMapping("shopCart")
@Slf4j
public class ShopCartController {
    private final ShopCartService shopCartService;

    @Autowired
    public ShopCartController(ShopCartService shopCartService) {
        this.shopCartService = shopCartService;
    }

    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST")
    @PostMapping("/add")
    public ServerResult add(@RequestParam String userId,
                            @RequestBody ShopCartBO shopCartBO) {
        if (StrUtil.isBlank(userId)) {
            return ServerResult.errorMsg(null);
        }
        log.info(shopCartBO.toString());
        shopCartService.addToShopCart(shopCartBO, userId);
        return ServerResult.ok();
    }

    @ApiOperation(value = "从购物车中删除商品",notes = "从购物车中删除商品",httpMethod = "POST")
    @PostMapping("/del")
    public ServerResult del(@RequestParam String userId,
                            @RequestParam String itemSpecId){
        if(StrUtil.isBlank(userId) || StrUtil.isBlank(itemSpecId)){
            return ServerResult.errorMsg(null);
        }
        shopCartService.delFromShopCart(itemSpecId, userId);
        return ServerResult.ok();
    }
}
